package top.superflow.flow.condition.exl

import org.apache.commons.jexl3.{JexlContext, JexlExpression, MapContext}
import top.superflow.exception.Exceptable
import top.superflow.lang.CollectionUtils

class ExtExpressionCondition (jexlExpression: JexlExpression) extends Exceptable{

  if(jexlExpression == null){
    throwRequestUnsupportedException("The jexlExpression can't be null")
  }

  def evaluate(parameters : Map[String, Any]) : Any = {
    val context = getContext(parameters)
    jexlExpression.evaluate(context)
  }

  private def getContext(parameters : Map[String, Any]) : JexlContext = {
    val context = new MapContext
    if(CollectionUtils.isEmpty(parameters)){
      return context
    }

    for((paramName, paramValue) <- parameters){
      context.set(paramName, paramValue)
    }
    return context
  }

  def getSourceTest : String = {
    jexlExpression.getSourceText
  }

  def getParsedTest : String = {
    jexlExpression.getParsedText
  }
}
